// 前端发送请求之后后端显示的部分,做数据库的操作例如增删改查
const connection = require('../app/database')

class UserService {
  async create(user){
    const {name, password} = user;
    //将user存储到数据库中
    const statement = `INSERT INTO users (name, password) VALUES (?, ?);`
    // 执行SQL语句
    const result = await connection.execute(statement, [name, password])

    return result[0];
  }
  // 查询注册名字是否重复的SQL语句
  async getUserByName(name){  
    const statement = `SELECT * FROM users WHERE name = ?;`;
    const result = await connection.execute(statement, [name])
    return result[0];
  } 
  async updateAvatarUrlById(avatarUrl, userId){
    const statement = `UPDATE users SET avatar_url = ? WHERE id = ?;`;
    const [result] = await connection.execute(statement, [avatarUrl, userId])
    return result
  }
}
 



module.exports = new UserService()